/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.analysis.Analysis;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.AnalysisSyncBroker;
import systole.synchronization.remote.entities.AnalysisRemote;

/**
 *
 * @author jmj
 */
public class AnalysisSyncBrokerDB extends BrokerDB implements AnalysisSyncBroker {

    public void saveAnalysisRemote(AnalysisRemote analysisRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote analysis");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(analysisRemote);
            this.logger.logDebug("remote analysis saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save analysis remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<AnalysisRemote> getAnalysisToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Analysis> getAnalysisToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting analysis to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select a from AnalysisRemote ar right join ar.analysis a where ar=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Analysis> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get analysis to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los análisis", e.fillInStackTrace());
        }
    }

    public Analysis getAnalysisByRemoteId(int remoteId) throws ExceptionDAO {
         try {
            this.logger.logDebug("getting analysis by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    AnalysisRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            AnalysisRemote analysisRemote = (AnalysisRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (analysisRemote != null ? analysisRemote.getAnalysis() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get analysis by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los análisis", e.fillInStackTrace());
        }
    }

    public AnalysisRemote getAnalysisRemoteByAnalysis(Analysis analysis) throws ExceptionDAO {
          try {
            this.logger.logDebug("getting analysis remote by local analysis");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    AnalysisRemote.class).add(Restrictions.eq("analysis", analysis)).setMaxResults(1);
            AnalysisRemote sportRemote = (AnalysisRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return sportRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote analysis by analysis, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los análisis", e.fillInStackTrace());
        }
    }

    public int getLastRemoteAnalysisIdSynchronized() throws ExceptionDAO {
         try {
            this.logger.logDebug("getting last remote analysis sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    AnalysisRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            AnalysisRemote analysisRemote = (AnalysisRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (analysisRemote != null ? analysisRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote analysis synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los análisis", e.fillInStackTrace());
        }
    }


}
